package com.xz.list;

/**
 * @author XZ
 * @create 2021-04-18 21:43
 */
public abstract class AbstractList<E> implements List {
    //元素的数量
    protected int size;

    @Override
    public void add(int index, Object element) {

    }

    protected void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
    }

    protected void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            this.outOfBounds(index);
        }
    }

    protected void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean contains(Object element) {
        return indexOf(element) != ELEMENT_NOT_FOUND;
    }

    public void add(Object element) {
        add(size, element);
    }

}
